﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using ServerMonitor.Models;

namespace ServerMonitor
{
    public partial class SelectMonitorItem : Form
    {
        Form1 f = null;
        public SelectMonitorItem(Form1 form)
        {
            try
            { 
                InitializeComponent();
                f = form;
                List<ProcessDescribe> pds = new List<ProcessDescribe>();
                Process[] pro = Process.GetProcesses();
                ProcessDescribe pd = null;
                foreach (var item in pro)
                {
                    try
                    {
                        pd = new ProcessDescribe();
                        pd.ProcessName = item.ProcessName;
                        try
                        {
                            pd.StartTime = item.StartTime;
                        }
                        catch  { } 
                        pd.ProcessPath = item.MainModule.FileName; 
                        // pd.MainWindowTitle = item.MainWindowTitle;
                        pd.Responding = item.Responding == true ? "运行中" : "停止运行"; 

                        if (!pds.Any(a => a.ProcessPath.Equals(pd.ProcessPath)))
                        {
                            if (!f.setting.Pds.Any(b => b.ProcessPath.Equals(pd.ProcessPath)))
                            {
                                pds.Add(pd);
                            }
                        }
                    }
                    catch (Exception ex) { }
                } 
                dataGridView1.AutoGenerateColumns = false; 
                dataGridView1.DataSource = pds; 
            }
            catch (Exception ex)
            { }
        }
        private void btAdd_Click(object sender, EventArgs e)
        {
            try
            {
                List<KeyValuePair<string, string>> kv = new  List<KeyValuePair<string,string>> ();
                List<string> addProcess = new List<string>();
                DataGridViewSelectedRowCollection items = dataGridView1.SelectedRows;
                foreach (DataGridViewRow item in items)
                {
                    ProcessDescribe selected = item.DataBoundItem as ProcessDescribe;



                    var old =f.setting.Pds.SingleOrDefault(a=>a.ProcessPath.Equals(selected.ProcessPath));//查看是否已经在监控列表中 

                    if (old == null)
                    {
                        f.setting.Pds.Add(new ProcessDescribe
                        {
                            // MainWindowTitle = selected.MainWindowTitle,
                            ProcessName = selected.ProcessName,
                            Responding = selected.Responding,
                            StartTime = selected.StartTime,
                            Monitor = false,
                             Memo=string.Empty,
                            ProcessLavel = 0,
                            ProcessPath = selected.ProcessPath

                        });


                        Common.LogHelper.Log("添加监控程序,名称：" + selected.ProcessName + ",路径：" + selected.ProcessPath);
                    }
                    else
                    {
                        old.StartTime = selected.StartTime;
                    } 
                } 
                Form rename = new ReName(f);
                rename.ShowDialog(); 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            this.DialogResult = DialogResult.OK;
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        } 






















    }
}
